<template>
  <div>
    <el-form :model="fromData" :rules="rules">
      <el-form-item prop="username" label="手机号">
        <el-input v-model="fromData.username" />
      </el-form-item>
      <el-form-item prop="password" label="密码">
        <el-input v-model="fromData.password" />
      </el-form-item>
      <el-form-item prop="password2" label="再次输入密码">
        <el-input v-model="fromData.password2" />
      </el-form-item>
      <el-form-item>
        <el-button>登录</el-button>
      </el-form-item>
    </el-form>
  </div>
</template>

<script>
export default {
  data() {
    const checkPassword2 = (rule, value, callback) => {
      if (value === this.fromData.password) {
        callback()
      } else {
        callback(new Error('两次输入密码不一致'))
      }
    }
    return {
      fromData: {
        username: '',
        password: '',
        password2: ''
      },
      rules: {
        username: [
          { required: true, message: '内容不能为空', trigger: 'blur' },
          {
            pattern: /^1[3-9]\d{9}$/,
            message: '请输入合法手机号',
            trigger: 'blur'
          }
        ],
        password: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { min: 6, max: 16, message: '密码长度在 6-16 之间', trigger: 'blur' }
        ],
        password2: [
          { required: true, message: '该项不能为空', trigger: 'blur' },
          { trigger: 'blur', validator: checkPassword2 }
        ]
      }
    }
  }
}
</script>

<style>
</style>
